package ar.com.spsolutions.splilabrary.view.application.configuration;

import java.util.Map;

import org.apache.log4j.Logger;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.impl.StdSchedulerFactory;

import ar.com.spsolutions.splibrary.services.QuartzRunner;
import ar.com.spsolutions.splilabrary.view.application.SPApplication;

public class QuartzConfigurator extends Configurator {
	protected static final Logger LOG = Logger
			.getLogger(QuartzConfigurator.class);

	public QuartzConfigurator(final SPApplication application) {
		super(application);
	}

	@Override
	protected void initialize(final SPApplication app) {
		final JobDetail job = new JobDetail();
		job.setName("quartzRunner");
		job.setJobClass(QuartzRunner.class);

		@SuppressWarnings("unchecked")
		final Map<String, Object> dataMap = job.getJobDataMap();
		dataMap.put("dueNotifyTask", app.getGeneralService()
				.getLoanBookService());

		final CronTrigger trigger = new CronTrigger();
		trigger.setName("notifyDueJob");
		try {
			trigger.setCronExpression("0/5 * * * * ?");

			final Scheduler scheduler = new StdSchedulerFactory()
					.getScheduler();
			scheduler.start();
			scheduler.scheduleJob(job, trigger);
		} catch (final Exception e) {
			QuartzConfigurator.LOG
					.warn("An exception occurre initializing quartz + "
							+ e.getMessage());
		}
	}

}
